﻿Imports System.Threading.Tasks
Imports SFDL.Core
Imports SFDL.PluginInterfaces

Module PluginHelper

    Friend CONST_PLUGINDIR As String = IO.Path.Combine(IO.Path.GetDirectoryName(Application.ExecutablePath), "plugins")
    Friend _plugin_connector As New PluginConnector
    Private _log As NLog.Logger = NLog.LogManager.GetLogger("PluginHelper")

    Friend ReadOnly Property GetPluginsPath As String
        Get
            Return CONST_PLUGINDIR
        End Get
    End Property

    Friend ReadOnly Property GetPluginConnectorInstance As PluginConnector
        Get
            Return _plugin_connector
        End Get
    End Property

    Friend Sub ProgressPostUnRarPlugins(_archive_file_list As List(Of String), _volume_files As List(Of String), _extract_dir As String, _unrar_sucessful As Boolean)

        Try

            If IsNothing(GetPluginConnectorInstance.PostUnRARPlugins) Then
                Throw New Exception("Keine PostUnRAR Plugins geladen!")
            End If

            For Each _plugin As IPostUnRARPlugin In GetPluginConnectorInstance.PostUnRARPlugins
                _plugin.PostUnRAR(_archive_file_list, _volume_files, _extract_dir, _unrar_sucessful)
            Next


        Catch ex As Exception
            _log.Error(ex, ex.Message)
        End Try


    End Sub

    Friend Function ProgressClicknLoadPlugins(ByVal _url As String) As String

        Dim _tmp_param_str As String = String.Empty
        Dim _file As String = String.Empty

        _tmp_param_str = _url

        _tmp_param_str = _tmp_param_str.Replace("sfdl://", "") 'URI Handler Prefix entfernen

        ' If _tmp_param_str.Contains("http://") Or _tmp_param_str.Contains("www.") Then 'handelt sich um eine URL --> ClicknLoad

        Try

            If IsNothing(GetPluginConnectorInstance.ClicknLoadPlugins) Then
                Throw New Exception("Keine ClicknLoad PLugins geladen!")
            End If

            For Each _plugin As IClicknLoadPlugin In GetPluginConnectorInstance.ClicknLoadPlugins

                If _plugin.URLSupported(_tmp_param_str) Then

                    _file = _plugin.DownloadSFDL(_tmp_param_str)

                    Throw New PluginFoundException("Plugin gefunden!")

                End If

            Next

        Catch ex As PluginFoundException

        Catch ex As Exception
            _log.Error(ex, ex.Message)
        End Try

        ' End If

        Return _file

    End Function

    Friend Sub ProgressCommandButtonPlugins()

        Try
            If IsNothing(GetPluginConnectorInstance.ClicknLoadPlugins) Then
                Throw New Exception("Keine ClicknLoad PLugins geladen!")
            End If

            For Each _plugin As ICustomCommandPlugin In GetPluginConnectorInstance.CustomCommandPlugins

                Try

                    Dim _toolstrip_item As ToolStripItem

                    _toolstrip_item = frm_main.ExtrasToolStripMenuItem.DropDownItems.Add(_plugin.GetCommandButtonName)

                    AddHandler _toolstrip_item.Click, AddressOf _plugin.CommandButtonEvent

                Catch ex As Exception
                    _log.Error(ex, ex.Message)
                End Try

            Next

        Catch ex As Exception
            _log.Error(ex, ex.Message)
        End Try

    End Sub

    Friend Sub RefreshCustomCommandPlugin(ByVal _plugin_connector As PluginConnector)

        Parallel.ForEach(_plugin_connector.CustomCommandPlugins, (Sub(_plugin As ICustomCommandPlugin)

                                                                      _plugin.SFDLSessionList = _var_sfdl_session_list
                                                                      _plugin.SettingsPath = GetSettingsPath
                                                                      _plugin.LoaderSettings = GetSettings

                                                                  End Sub))

    End Sub

    Friend Sub InitializePlugins(ByVal _plugin_connector As PluginConnector)

        Parallel.ForEach(_plugin_connector.ClicknLoadPlugins, (Sub(_plugin As IClicknLoadPlugin) _plugin.Initialize()))

        Parallel.ForEach(_plugin_connector.CustomCommandPlugins, (Sub(_plugin As ICustomCommandPlugin)

                                                                      _plugin.SFDLSessionList = _var_sfdl_session_list
                                                                      _plugin.SettingsPath = GetSettingsPath
                                                                      _plugin.LoaderSettings = GetSettings

                                                                  End Sub))

        Parallel.ForEach(_plugin_connector.CustomCommandPlugins, (Sub(_plugin As ICustomCommandPlugin) _plugin.Initialize()))

        Parallel.ForEach(_plugin_connector.PostUnRARPlugins, (Sub(_plugin As IPostUnRARPlugin) _plugin.Initialize()))

    End Sub

End Module